<?php


namespace App\Http\Controllers\App;


use App\Http\Controllers\Controller;
//use Yansongda\Pay\Pay;
use App\Tool\Util;
use Illuminate\Http\Request;
use Yansongda\LaravelPay\Facades\Pay;

class PayController extends Controller
{


    public function pay(Request $request){

//        $pay_type = $request->any("pay_type"); // 支付方式 1微信，2支付宝，3余额支付
        $pay_type = 1;
        if (!$pay_type) {
            $pay_type == 1;
        }

        $out_trade_no = Util::getOrderNum();
        $body = 'subject-测试';
        if ($pay_type == 1) {
            $wechat = Pay::wechat();
            $order = [
                'out_trade_no' => $out_trade_no,
                'body' => $body,
                'total_fee' => '1',
                'attach' => 1,
            ];
            $params = $wechat->app($order)->getContent();
        }

        return $this->renderJson(['pay_type' => $pay_type, 'params' => $params]);
    }

    //微信支付通知
    public function wxnotify()
    {
        $wechat = Pay::wechat();

        try {
            \DB::beginTransaction();
            $data = $wechat->verify();
            $callback = $data->all();
            if ($callback['result_code'] === 'SUCCESS' && $callback['return_code'] === 'SUCCESS') {


                var_dump($callback);


            }
            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            // 记录错误日志
            var_dump($e->getMessage());
        }
        return $wechat->success();
    }

}